﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FullImageView.xaml
    /// </summary>
    public partial class TopCommandBar : UserControl
    {
        public event EventHandler WantViewFullImage;
        public event EventHandler WantCompare;
        public event EventHandler WantRemoveDuplicates;        
        public event EventHandler<ObjectEventArgs<double, double>> ChangeThumbnailSize;

        public TopCommandBar()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(TopCommandBar_Loaded);
        }

        void TopCommandBar_Loaded(object sender, RoutedEventArgs e)
        {
            txtHeight.Text = Settings.Values.ThumbHeight.ToString("N0");
            txtWidth.Text = Settings.Values.ThumbWidth.ToString("N0");
        }

        protected void OnWantRemoveDuplicates()
        {
            if (WantRemoveDuplicates != null)
                WantRemoveDuplicates(this, EventArgs.Empty);
        }

        protected void OnWantViewFullImage()
        {
            if (WantViewFullImage != null)
                WantViewFullImage(this, EventArgs.Empty);
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            OnWantViewFullImage();
        }

        public void SetThumbnailWidthHeight()
        {
            double w, h;

            if (!double.TryParse(txtWidth.Text, out w) || !double.TryParse(txtHeight.Text, out h))
                return;

            Settings.Values.SetThumbWidthAndHeight(w, h);
        }

        private void txtWidth_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetThumbnailWidthHeight();
            }
        }

        private void txtHeight_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SetThumbnailWidthHeight();
            }
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            OnWantRemoveDuplicates();
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            if (WantCompare != null)
                WantCompare(this, EventArgs.Empty);
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            double width;
            double height;

            if (!double.TryParse(txtWidth.Text, out width))
            {
                txtWidth.Focus();
                return;
            }

            if (!double.TryParse(txtHeight.Text, out height))
            {
                txtHeight.Focus();
                return;
            } 
            
            if (ChangeThumbnailSize != null)
                ChangeThumbnailSize(this, new ObjectEventArgs<double, double>(width, height));
        }
    }
}
